
package com.yuexin.common.utils;

import java.net.URLDecoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import javax.crypto.spec.SecretKeySpec;

import sun.misc.BASE64Decoder;

/**
 * 类名: ThreeDES <br/> 
 * 功能: ThreeDES算法 <br/> 
 * 创建日期: 2014-3-3 上午11:17:12 <br/> 
 *
 * @author 
 * @version V3.0
 * @since Jdk 1.6
 * @see       
 *
 */

public class ThreeDES {

private static final String Algorithm = "DESede"; // DES,DESede,Blowfish
    
    public static byte[] encryptMode(byte[] keybyte, byte[] src) {
       try {
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.ENCRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

    public static byte[] decryptMode(byte[] keybyte, byte[] src) {      
    try {
            SecretKey deskey = new SecretKeySpec(keybyte, Algorithm);
            Cipher c1 = Cipher.getInstance(Algorithm);
            c1.init(Cipher.DECRYPT_MODE, deskey);
            return c1.doFinal(src);
        } catch (java.security.NoSuchAlgorithmException e1) {
            e1.printStackTrace();
        } catch (javax.crypto.NoSuchPaddingException e2) {
            e2.printStackTrace();
        } catch (java.lang.Exception e3) {
            e3.printStackTrace();
        }
        return null;
    }

    public static String byte2hex(byte[] b) {
        String hs="";
        String stmp="";
        for (int i=0; i < b.length; i++) {
            stmp=(Integer.toHexString(b[i] & 0XFF));
            if (stmp.length() == 1)
            	hs += ("0" + stmp);
            else 
            	hs += stmp;
        }
        return hs.toUpperCase();
    }

    public static String byte2Hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}
    
	public static byte[] hex2Byte(String str) {
		if (str == null)
			return null;
		str = str.trim();
		int len = str.length();
		if (len == 0 || len % 2 == 1)
			return null;
		byte[] b = new byte[len / 2];
		try {
			for (int i = 0; i < str.length(); i += 2) {
				b[i / 2] = Integer.decode("0x" + str.substring(i, i + 2)).byteValue();
			}
			return b;
		} catch (Exception e) {
			return null;
		}
	}
    
    public static String bytesToHexString(byte[] b){
    	StringBuffer sb = new StringBuffer();
    	for (int i = 0; i < b.length; i++) {
    		sb.append(Integer.toHexString(b[i]));
		}
    	return sb.toString().toUpperCase();
    }
    
    //3DESECB解密,key必须是长度大于等于 3*8 = 24 位  
	public static String decryptThreeDESECB(String src,String key) throws Exception  
    {  
    	//--通过base64,将字符串转成byte数组  
        BASE64Decoder decoder = new BASE64Decoder();  
        byte[] bytesrc = decoder.decodeBuffer(src);  
        //--解密的key  
        DESedeKeySpec dks = new DESedeKeySpec(key.getBytes("UTF-8"));  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");  
        SecretKey securekey = keyFactory.generateSecret(dks);  
          
        //--Chipher对象解密  
        Cipher cipher = Cipher.getInstance("DESede/ECB/PKCS5Padding");  
        cipher.init(Cipher.DECRYPT_MODE, securekey);  
        byte[] retByte = cipher.doFinal(bytesrc);  
          
        return new String(retByte,"UTF-8"); 
    }  
    
   public static void main(String[] args) throws Exception  {
	   
	//String firString = "QusagUYAeF7e6tKOCkYLpbevG4JizicrEJhyYV7/RmplfyOXtwEqcoFN8ieTgJkKTAP5ip3xYSKkT75lJxq1sLg7VihHrA5+WZheDVzCPAAnDSEGioxggCn2gXu745A7ZkTMbijWZFsrQ/21V7vK6j8IVtlj0D5dzsUaIh7d+C7GcQtEOQFu5P4hdeOCL1cfwgfJCg0V8ZyVk86lqeAoI68OOimlCuAtfEsw84+eBNE=";
	
	//后台接受时
	//String urlParam = "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FDfn8Dl1msf5DgbD5Rs60Jsgd4R7x2L5kduz1cjYaVaO36yNfzn0YpnIu1dLTxuzxKju2mjNm9KbcprhnUo%2FgjLacBSIN4BECFRltkFzr39hJWI8yOU%2FS8rVVxFsAeZgr0XT7zFqE";
	
	String kkk = "QusagUYAeF7e6tKOCkYLpbevG4JizicrEJhyYV7/RmplfyOXtwEqcmoJKSrF7508tA6r+K7mxZM/E/hi98N0vbxY77Qazumz4SSIqtkbjtectA3pPvZfV+XVVYSYPz95KlrONmX/STciFuB0LjV68YoSKfbSPdWfaHi9SBo6tGINM2Y7hNOBvpSpKfj0EEp26SHt8Mji9rX3rDryHhkDWxrl8z+ht3Rtuu2i7XtGwqX0u61M1c+oeW8HveVwmr8synHFAqzbHc7coEonp0MEX3R1osFKqpGX";
	
	String lll = "QusagUYAeF7e6tKOCkYLpbevG4JizicrEJhyYV7%2FRmplfyOXtwEqcmoJKSrF7508tA6r%2BK7mxZM%2FE%2Fhi98N0vbxY77Qazumz4SSIqtkbjtectA3pPvZfV%2BXVVYSYPz95KlrONmX%2FSTciFuB0LjV68YoSKfbSPdWfaHi9SBo6tGINM2Y7hNOBvpSpKfj0EEp26SHt8Mji9rX3rDryHhkDWxrl8z%2Bht3Rtuu2i7XtGwqX0u61M1c%2BoeW8HveVwmr8synHFAqzbHc7coEonp0MEX3R1osFKqpGX";
	
	/*String encodedParam = URLEncoder.encode(kkk, "UTF-8");
	System.out.println("encodedParam=="+encodedParam);*/
	
	String decodedParam = URLDecoder.decode(lll, "UTF-8");
	System.out.println("decodedParam=="+decodedParam);
	
	String decodefirString = ThreeDES.decryptThreeDESECB(kkk,"be80cb80a4f3e7225209a083e53e3ac0"); 
	System.out.println(decodefirString);
	
}
}
